﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Exceptions;
using WmsApp.Service;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormPayType : BaseForm
    {
        public FormPayType()
        {
            InitializeComponent();
            Lookup.LookupBind.BindingLookUpEditIsAlive(this.gridViewMain, this.gridColumn3);
            this.gridViewMain.CustomRowCellEdit += GridViewMainCustomRowCellEdit;
            CmdQuery_Click(null, null);
        }

        public override string HeadText
        {
            get { return "付款类型管理"; }
        }

        void GridViewMainCustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.gridColumn1)
            {
                var payType = this.gridViewMain.GetRow(e.RowHandle) as PayType;
                if (payType != null)
                {
                    if (payType.WcfEntityState != EntityState.Added)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                }
            }
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            var client = EntityServiceFactory.GetEntityService<PayType, PayTypeService>();
            var list = client.GetEntityList();
            this.gridControlMain.DataSource = list;
        }

        private void CmdNew_Click(object sender, EventArgs e)
        {
            var payType = PayType.CreatePayType("请更改代码");
            if (this.gridControlMain.DataSource == null)
            {
                this.gridControlMain.DataSource = new List<PayType>();
            }
            ((List<PayType>)this.gridViewMain.DataSource).Add(payType);
            this.gridControlMain.RefreshDataSource();

            for (int idx = 0; idx < ((List<PayType>)this.gridViewMain.DataSource).Count; idx++)
            {
                var u = (PayType)this.gridViewMain.GetRow(idx);

                if (u == payType)
                {
                    this.gridViewMain.FocusedRowHandle = idx;
                }
            }
        }

        private void CmdSave_Click(object sender, EventArgs e)
        {
            try
            {
                EntityServiceFactory.GetEntityService<PayType, PayTypeService>().
                SaveEntityList(EntityControl.EntityFilterByState.GetNeedSaveEntity(this.gridViewMain.DataSource as List<PayType>));
                XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
            }
        }
    }
}